package com.yidu.hbatsams.action.AtsScheme;

import com.yidu.hbatsams.domain.*;
import com.yidu.hbatsams.service.AtsSchemeServicefx;
import com.yidu.hbatsams.utils.Message;
import org .springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/atsScheme")
public class AtsSchemeActionfx {
     @Resource
    private AtsSchemeServicefx atsSchemeServicefx;

     @RequestMapping("/findAll")
     @ResponseBody
    public Map<String,Object> findAll(@RequestParam("page") Integer pageNum, @RequestParam("limit")Integer pageSize,String schemeType,String schemeName,String stime,String etime,Integer coachId,Integer schemeId,String athleteId,Integer expertId,Integer num,HttpSession session){
         Map<String,Object> map  = new HashMap<>();
         //获取总行数
         int count = atsSchemeServicefx.count(schemeType,schemeName,stime,etime,session);

         List<AtsScheme> list = atsSchemeServicefx.findAll(pageSize,pageNum,schemeType,schemeName,stime,etime,coachId,schemeId,athleteId,expertId,num,session);

         map.put("msg","");
         map.put("code",0);
         map.put("count",count);

         map.put("data",list);
         return map;
     }
    @RequestMapping("/updateId")
    @ResponseBody
    public Message updateId(String schemeId){
         Message message = new Message();
         message.setNum(atsSchemeServicefx.updateId(Integer.valueOf(schemeId)));
         return message;
    }
    @RequestMapping("/insert")
    @ResponseBody
     public Message insert(AtsScheme atsScheme, MultipartFile file,MultipartFile filetwo,MultipartFile filethird,HttpSession session){

         int row = 0;
         Message message = new Message();
         if(atsScheme.getSchemeId()==null){
             try {
                 row = atsSchemeServicefx.insertSelective(atsScheme,file,filetwo,filethird,session);
             } catch (ParseException e) {
                 e.printStackTrace();
             }
         }else if(atsScheme.getSchemeId() !=null){
             row = atsSchemeServicefx.updateByPrimaryKey(atsScheme,file,filetwo,filethird);
         }
         if(row>0){
             message.setMsg("yes");
             message.setNum(1);
         }else{
             message.setMsg("no");
             message.setNum(0);
         }
         return message;
     }
    @RequestMapping("/findselect")
    @ResponseBody
     public List<AtsEmpCoach> findSelect(String coachName){
         List<AtsEmpCoach> list = atsSchemeServicefx.findAllselect(coachName);
         return list;
     }
    @RequestMapping("/findselects")
    @ResponseBody
     public List<AtsEmpAthlete> findAllSelect(HttpSession session){
         List<AtsEmpAthlete> list = atsSchemeServicefx.findAllselects(session);
         return list;
     }
     @RequestMapping("/findCheckbox")
     @ResponseBody
     public List<AtsSchemeAthlete> findCheckbox(Integer schemeId){
          List<AtsSchemeAthlete> list = atsSchemeServicefx.selectCheckbox(schemeId);

          return list;
     }
     @RequestMapping("/updateisRead")
     @ResponseBody
    public int updateisRead(Integer schemeId, Integer num, HttpSession session){

         return atsSchemeServicefx.updateisRead(schemeId,num,session);
     }
    @RequestMapping("/findById")
    @ResponseBody
     public AtsScheme findById(Integer schemeId){
         AtsScheme atsScheme = atsSchemeServicefx.findById(schemeId);
        return  atsScheme;
     }
    /**
     * 专家咨询：审核
     * @param expert
     * @param audit
     * @return
     */
    @RequestMapping("/insertAdvice")
    @ResponseBody
    public Message insertAdvice(Integer schemeId,AtsSchemeExpert expert, AtsSchemeAudit audit){
        int row = 0;
        Message message = new Message();

        List<AtsSchemeExpert> e = atsSchemeServicefx.findExperts(schemeId);
        List<AtsSchemeAudit> a = atsSchemeServicefx.findAduits(schemeId);
        if(e.size()>0 && a.size()>0) {
            row = atsSchemeServicefx.updateAdvice(expert,audit,schemeId);

        }else{
            row = atsSchemeServicefx.insertAdvice(expert, audit,schemeId);

        }



        if(row>0){
            message.setMsg("yes");
            message.setNum(1);
        }else{
            message.setMsg("no");
            message.setNum(0);
        }

        return message;
    }

    /**
     * 反馈
     * @param record
     * @param file
     * @param schemeId
     * @return
     * @throws IOException
     */
    @RequestMapping("/insertResult")
    @ResponseBody
    public Message insertResult(AtsScheme record, MultipartFile file,Integer schemeId) throws IOException {
        int row = 0;
        Message message = new Message();
        if(record.getSchemeId()!=null){
            row = atsSchemeServicefx.insertResult(record,file,schemeId);
        }
        if(row>0){
            message.setMsg("yes");
            message.setNum(1);
        }else{
            message.setMsg("no");
            message.setNum(0);
        }
        return message;
    }

    /**
     * 审核；查询专家表需要信息
     * @param schemeId
     * @return
     */
    @RequestMapping("/findExpert")
    @ResponseBody
    public List<AtsSchemeExpert> findExpert(Integer schemeId) {
        List<AtsSchemeExpert> expert = atsSchemeServicefx.findExperts(schemeId);

        return expert;
    }
    @RequestMapping("/huifuExpert")
    @ResponseBody
    public Message huifuExpert(AtsSchemeExpert expert,AtsSchemeAudit audit,Integer schemeId) {
        int row = 0;

        Message message = new Message();
        row=atsSchemeServicefx.huifuExpert(expert,audit,schemeId);
        if(row>0){
            message.setMsg("yes");
            message.setNum(1);
        }else{
            message.setMsg("no");
            message.setNum(0);
        }
        return message;
    }
    @RequestMapping("/findByIdChakanExpertQuanxian")
    @ResponseBody
    public AtsSchemeExpert findByIdChakanExpertQuanxian(Integer schemeId){
        AtsSchemeExpert atsSchemeExpert = atsSchemeServicefx.findByIdChakanExpertQuanxian(schemeId);
        return atsSchemeExpert;
    }
    @RequestMapping("/findByIdChakanAthleteQuanxian")
    @ResponseBody
    public AtsSchemeAthlete findByIdChakanAthleteQuanxian(Integer schemeId) {
        System.out.println(schemeId+"..");
        AtsSchemeAthlete atsSchemeAthlete = atsSchemeServicefx.findByIdChakanAthleteQuanxian(schemeId);

        return atsSchemeAthlete;
    }
}
